package com.kfpanda.search.index;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.kfpanda.search.index.context.IndexBean;
import com.kfpanda.search.index.context.ResourceBean;

/**
 * <b>ClassName:<b> IndexJob
 * <br><b>Description: <br>
 * 一个索引创建job。
 * @Author: kfpanda
 * @Date：2012-2-6 上午12:40:34
 * @ModifyUser：shapecool
 *
 */
public class IndexJob implements Job{
	private Logger logger = Logger.getLogger(IndexJob.class);
	@Resource
	private IndexFile indexFiles;
	private String indexPath;
	private File INDEX_DIR;

	public void indexCreate(String filePath) {
		if (StringUtils.isBlank(filePath)) {
			return;
		}
		if (INDEX_DIR == null) {
			INDEX_DIR = new File(indexPath);
		}
		if (!INDEX_DIR.exists()) {
			logger.setResourceBundle(null);
			logger.error("索引文件保存目录（" + INDEX_DIR + "），无法找到。");
		}

		final File docDir = new File(filePath);
		if (!docDir.exists() || !docDir.canRead()) {
			logger.debug("Document directory '" + docDir.getAbsolutePath()
					+ "' does not exist or is not readable, please check the path");
		}

		Date start = new Date();
		try {
//			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
			Analyzer analyzer = new IKAnalyzer();// 采用的分词器
			//第三个参数 为true表示新建，false表示添加到原有索引中
			IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR),
					analyzer, true,
					IndexWriter.MaxFieldLength.LIMITED);
			logger.debug("Indexing to directory '" + INDEX_DIR + "'...");
			indexFiles.indexDocs(writer, docDir);
			logger.debug("Optimizing...");
			writer.optimize();
			writer.close();

			Date end = new Date();
			logger.debug(end.getTime() - start.getTime() + " total milliseconds");

		} catch (IOException e) {
			logger.error(" caught a " + e.getClass() + "\n with message: ", e);
		}
	}
	
	public void initParam(IndexBean indexBean, ResourceBean resourceBean){
		this.indexPath = indexBean.getPath();
	}
	
	/* (non-Javadoc)
	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
	 */
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		JobDataMap dataMap = context.getJobDetail().getJobDataMap();
		IndexBean indexBean = (IndexBean)dataMap.get("indexBean");
		ResourceBean resourceBean = (ResourceBean)dataMap.get("resourceBean");
		this.indexPath = indexBean.getPath();
		System.out.println("----------------------");
		System.out.println("----------------------");
//		this.indexCreate("");
//		this.initParam(indexBean, resourceBean);
//		List<ResourceBean> resourceList = indexBean.getResources();
//		if(resourceList == null){
//			return ;
//		}
//		for(int i = 0, len = resourceList.size(); i < len; i++){
//			ResourceBean resource = resourceList.get(0);
//			this.indexCreate(resource.getPath());
//		}
	}

}
